/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trong.gold.soft.com.view;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import trong.gold.soft.com.main.mTetrisMidlet;
import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.createImg;
import trong.gold.soft.com.model.font.*;

/**
 *
 * @author Admin
 */
public class LoadingScreen extends Canvas {

    public String str = "Đang tải...";
    public Image imgLogo;
    public int loadingCurrent = 0;
    private String imgName="logo.png";
    int getX = 0, getY = 0;
    mTetrisMidlet midlet;
    boolean run = true;
    tThread t;
    int h, w;

    public LoadingScreen(mTetrisMidlet midlet) {
        setFullScreenMode(true);
        this.midlet = midlet;
        System.out.println("splashScreen");
        run = true;
        h = this.getHeight();
        w = this.getWidth();
        imgLogo = createImg.createImage("/com/image/load/" + imgName);
        getX = getWidth() / 2 - imgLogo.getWidth() / 2;
        getY = getHeight() / 2 - imgLogo.getHeight() / 2 - 20;
        t = new tThread();
        t.start();
    }

    public void paint(Graphics g) {
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, w, h);
        g.drawImage(imgLogo, getX, getY, Graphics.TOP | Graphics.LEFT);
        VietnameseFont.White.DrawString(g, str, getWidth() / 2 - VietnameseFont.Black.getWidth(str) / 2, getY + imgLogo.getHeight() + 20, 0);
        g.setColor(Resources.white);
        g.fillRoundRect(getWidth() / 2 - 130 / 2, getY + imgLogo.getHeight() + 10, 130, 6, 6, 6);

        g.setColor(Resources.orange2);
        g.fillRoundRect(getWidth() / 2 - 130 / 2, getY + imgLogo.getHeight() + 10, loadingCurrent, 6, 6, 6);

        g.setColor(Resources.white);
        g.drawRoundRect(getWidth() / 2 - 130 / 2, getY + imgLogo.getHeight() + 10, 130, 6, 6, 6);
        Runtime.getRuntime().gc();
    }

    /**
     * Default timeout with thread
     */
    public void setNull() {                
        this.imgLogo = null;
        this.t = null;
        this.str = null;
        Runtime.getRuntime().gc();
    }

    public void nextScreen() {
        setNull();
        Runtime.getRuntime().gc();
        midlet.switchState(this, new slSound(midlet));
    }

    class tThread extends Thread {

        public void run() {
            while (run) {
                loadingCurrent += 6;
                //str = "Đang tải...";
                if (loadingCurrent > 130) {
                    run = false;
                    nextScreen();
                }
                repaint();
                Runtime.getRuntime().gc();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
